package com.xs.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xs.bean.UserBean;
import com.xs.bean.VideoTableBean;
import com.xs.service.VideoService;
import com.xs.serviceimpl.VideoServiceImpl;

@WebServlet("/VideoServlet")
public class VideoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VideoService vs;

	public VideoServlet() {
		vs = new VideoServiceImpl();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求跟相应的编码
		request.setCharacterEncoding("Utf-8");
		response.setCharacterEncoding("Utf-8");
		// 从login页面hidden隐藏域得到name="method"参数,通过if判断将要实现什么功能
		String method = request.getParameter("method");
		
		// 展现列表:根据用户id查找该用户上传的视频
		// "info".equals(method)是从homePage超链接过来的
		if ("info".equals(method)) {
			// 获得指定用户下的所有视频信息
			this.getVideoInfoByUserId(request, response);
		} 
		
		// 添加上传视频到sql里面
		else if ("add".equals(method)) {
			//标题栏命名
			String pageName = request.getParameter("pageName")+".flv";			
			// 添加上传的视频到sql里面,这里主要的就是值要对应video_tb里面保持一致
			// 获得video表单属性
			String videoName = (String) request.getSession().getAttribute("fileName"); // 通过session里面set-UploadServlet
			String type = request.getParameter("videoType"); // 在upload.jsp的 name提交获得
			String videoMsg = (String) request.getParameter("videoMsg"); // 在upload.jsp的 name提交获得
			String imgUrl = (String) request.getSession().getAttribute("ImgName"); // 在upload.jsp的 elem:'#imgUpload'跳转页面里set
			UserBean ub = (UserBean) request.getSession().getAttribute("ub"); // 通过session里面set-UserServlet
			int userId = ub.getUserId();
			vs.uploadVideo(pageName,videoName, Integer.parseInt(type), videoMsg, imgUrl, userId);
		} 
		
		//播放视频
		else if ("getVideo".equals(method)) {
			// 这里的"getVideo".equals(method)是从main.jsp超链接过来的
			// 通过指定用户获得该用户下指定的视频属性并转发到播放器
			this.getVideoById(request, response);
		}
	}

	private void getVideoInfoByUserId(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 通过用户id锁定用video户表里面的用户
		String id = request.getParameter("userId");
		//// 由于getParameter()方法获得的只能是String类型的值,所有要强转成int类型以对应
		int videoUserId = Integer.parseInt(id);
		// 获得该用户所有的视频信息并set,然后转发到"main.jsp"页面调用用户的视频属性
		List list = vs.getVideoInfoByUserId(videoUserId);
		request.setAttribute("videoInfo", list);
		for (int i = 0; i < list.size(); i++) {
			
			System.out.println(i);
		}
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}

	private void getVideoById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得指定播放的视频id
		String id = request.getParameter("videoId");
		// 由于getParameter()方法获得的只能是String类型的值,所有要强转成int类型以对应
		int videoId = Integer.parseInt(id);
		// 获得video并set,然后转发到play.jsp页面再调用video里面的属性
		VideoTableBean vb = vs.getVideoInfoById(videoId);
		request.setAttribute("video", vb);
		request.getRequestDispatcher("play.jsp").forward(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
